package com.morristaedt.mirror.modules.voice.handler;


import com.morristaedt.mirror.bean.SemanticResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by PR on 2017/8/8.
 */

public class OrderMenuHandler extends Handler {
    public static List<String> orders = new ArrayList<>();

    public OrderMenuHandler(SemanticResult result) {
        super(result);
    }


    @Override
    public String getFormatContent() {
        String intent = result.semantic.optString("intent");
        if(intent.equals("wantOrderIntent")){
            orders.clear();
            StringBuilder orderResponse = new StringBuilder();
            orderResponse.append(String.format("请选择您所在的地区分店"));
            orderResponse.append(NEWLINE);
            orderResponse.append(NEWLINE);
            orderResponse.append(NEWLINE);
            orderResponse.append(String.format("<a href=\"%s\">饭真香001分店</a>", "001"));
            orderResponse.append(NEWLINE);
            orderResponse.append(NEWLINE);
            orderResponse.append(String.format("<a href=\"%s\">饭真香002分店</a>", "002"));

            return orderResponse.toString();
        }else if(intent.equals("orderItemIntent")){
            String item = result.semantic.optJSONArray("slots").
                    optJSONObject(0).optString("value");
            orders.add(item);
            StringBuilder currentOrders = new StringBuilder();
            currentOrders.append("好的，当前您已经点了如下菜品：");
            currentOrders.append(NEWLINE);
            currentOrders.append(NEWLINE);
            currentOrders.append(NEWLINE);
            for(int index=0;index<orders.size();index++){
                currentOrders.append(String.format("%d. %s", index + 1, orders.get(index)));
                currentOrders.append(NEWLINE);
            }

            return currentOrders.toString();
        } else if(intent.equals("finishOrderIntent")){
            StringBuilder finishOrder = new StringBuilder();
            int priceSum = 0;
            finishOrder.append("好的，马上为您准备上菜，当前包含如下菜品：");
            finishOrder.append(NEWLINE);
            finishOrder.append(NEWLINE);
            finishOrder.append(NEWLINE);
            for(int index=0;index<orders.size();index++){
                int price = new Random().nextInt(20) + 20;
                finishOrder.append(String.format("%d. %s %d元",
                        index + 1,
                        orders.get(index),
                        price));
                priceSum += price;
                finishOrder.append(NEWLINE);
            }
            finishOrder.append(NEWLINE);
            finishOrder.append(NEWLINE);
            finishOrder.append(String.format("<strong>总价 %d元，请马上结账，我们不支持打白条或霸王餐</strong>"
                    , priceSum));
            return finishOrder.toString();
        }
        return result.answer;
    }

}
